package business_opportunity

import (
	"git.mycaigou.com/gfyx/common/authorize"
	"git.mycaigou.com/gfyx/common/rpc_client"
	"git.mycaigou.com/gfyx/micro-gfyx-mp-api/http_server/controller"
	"git.mycaigou.com/gfyx/micro-gfyx-mp-api/infrastructure/common/errcode"
	dto "git.mycaigou.com/gfyx/micro-gfyx-mp-api/infrastructure/dto/business_opportunity"
	"git.mycaigou.com/gfyx/micro-gfyx-mp-api/infrastructure/utils"
	"git.mycaigou.com/gfyx/micro-gfyx-mp-api/service/business_opportunity/customer"
	"git.mycaigou.com/gfyx/micro-gfyx-mp-api/service/business_opportunity/project"
	"git.mycaigou.com/gfyx/micro-gfyx-mp-api/service/clue/report"
	"git.myscrm.cn/golang/stark/v4"
	"github.com/gin-gonic/gin"
)

type CustomerController struct {
	controller.BaseController
	customerService customer.CustomerServiceIface
	reportService   report.ReportServiceIface
	landService     project.LandServiceIface
}

// NewCustomerController 客户池控制器
func NewCustomerController(
	customerService customer.CustomerServiceIface,
	reportService report.ReportServiceIface,
	landService project.LandServiceIface,
) *CustomerController {
	return &CustomerController{
		customerService: customerService,
		reportService:   reportService,
		landService:     landService,
	}
}

func (c *CustomerController) RegisterRouter(g *gin.RouterGroup) {
	// 找客户列表
	g.POST("/customer/list", c.list)
	// 客户详情
	g.GET("/customer/detail", c.detail)
	// 工程单位合作案例
	g.POST("/customer/supplier-project", c.getSupplierProject)
	// 获取好客户得分
	g.GET("/customer/get-developer-score-detail", c.getDeveloperScoreDetail)
	// 企业风险-失信被执行
	g.GET("/customer/zx-list", c.zxList)
	// 企业风险-立案信息
	g.GET("/customer/court-register", c.courtRegister)
	// 企业风险-法律诉讼
	g.GET("/customer/law-suit", c.lawSuit)
	// 企业风险-税收违法
	g.GET("/customer/tax-illegal-list", c.taxIllegalList)
	// 采购需求列表
	g.POST("/customer/bidding-list", c.getBiddingList)
	// 项目（合并）列表
	g.POST("/customer/project-lifecycle", c.getProjectLifecycle)
	// 客户动态列表
	g.POST("/customer/get-feed", c.getFeedList)
	// 投资企业、股权公司
	g.POST("/customer/get-invest-company", c.getInvestCompany)
	// 模糊搜索公司
	g.GET("/customer/fuzzy-search", c.fuzzySearch)
	// 企业风险-数量统计
	g.GET("/customer/risk-count", c.riskCount)
}

func (c *CustomerController) setAnonymityUser(ctx *gin.Context) {
	// 如果是未付费用户
	tenantCode := authorize.GetTenantCode(ctx)
	if tenantCode == "" {
		urlUid := ctx.Request.Header.Get("url_uid")
		c.reportService.SetSharerCodeWithReportUrl(ctx, urlUid)
	}
}

func (c *CustomerController) list(ctx *gin.Context) {
	req := &dto.GetCustomerListRequest{}
	if err := ctx.ShouldBind(req); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(req, err))
		return
	}

	if req.Page <= 0 {
		req.Page = 1
	}
	if req.PageSize <= 0 {
		req.PageSize = 20
	}
	resp, err := c.customerService.SearchCustomerList(ctx, req)
	if err != nil {
		c.Fail(ctx, errcode.COMMON_ERROR, err.Error())
		return
	}

	c.Success(ctx, resp)
}

func (c *CustomerController) detail(ctx *gin.Context) {
	c.setAnonymityUser(ctx)
	req := &dto.GetCustomerDetailRequest{}
	if err := ctx.ShouldBind(req); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(req, err))
		return
	}
	detail, err := c.customerService.GetCustomerDetail(ctx, req)
	if err != nil {
		grpcErr := rpc_client.GetGrpcError(err)
		c.Fail(ctx, grpcErr.GetCode(), grpcErr.GetMessage())
		return
	}

	c.Success(ctx, detail)
}

func (c *CustomerController) empty(ctx *gin.Context) {

	c.Success(ctx, "i am empty")
}

func (c *CustomerController) getSupplierProject(ctx *gin.Context) {
	c.setAnonymityUser(ctx)
	req := &dto.SupplierProjectRequest{}
	if err := ctx.ShouldBind(&req); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(req, err))
		return
	}

	res, err := c.customerService.GetSupplierProject(ctx, req)
	if err != nil {
		stark.Logger.Errorf(ctx, "customer/supplier-project err:%s", err.Error())
		c.Fail(ctx, errcode.COMMON_DATA_NOT_EXIST, "接口返回异常")
		return
	}
	c.Success(ctx, res)
}

func (c *CustomerController) getDeveloperScoreDetail(ctx *gin.Context) {
	c.setAnonymityUser(ctx)
	request := &dto.GetCustomerScoreDetailRequest{}
	if err := ctx.ShouldBind(request); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(request, err))
		return
	}

	res, err := c.customerService.GetDeveloperScoreDetail(ctx, request.OrganizingCode)
	if err != nil {
		stark.Logger.Errorf(ctx, "http getDeveloperScoreDetail request:%+v, err:%s", request, err.Error())
		c.Fail(ctx, errcode.COMMON_ERROR, "获取好客户得分失败")
		return
	}
	c.Success(ctx, res)
}

func (c *CustomerController) getBiddingList(ctx *gin.Context) {
	c.setAnonymityUser(ctx)
	req := &dto.GetBiddingListRequest{}
	if err := ctx.ShouldBind(&req); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(req, err))
		return
	}

	res, err := c.customerService.GetBiddingList(ctx, req)
	if err != nil {
		stark.Logger.Errorf(ctx, "customer/bidding-list err:%s", err.Error())
		c.Fail(ctx, errcode.COMMON_DATA_NOT_EXIST, "接口返回异常")
		return
	}
	c.Success(ctx, res)
}

func (c *CustomerController) getProjectLifecycle(ctx *gin.Context) {
	c.setAnonymityUser(ctx)
	req := &dto.GetProjectLifeCycleRequest{}
	if err := ctx.ShouldBind(&req); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(req, err))
		return
	}

	res, err := c.customerService.GetProjectLifeCycle(ctx, req)
	if err != nil {
		stark.Logger.Errorf(ctx, "customer/project-lifecycle err:%s", err.Error())
		c.Fail(ctx, errcode.COMMON_DATA_NOT_EXIST, "接口返回异常")
		return
	}
	c.Success(ctx, res)
}

func (c *CustomerController) getFeedList(ctx *gin.Context) {
	c.setAnonymityUser(ctx)
	req := &dto.GetFeedListRequest{}
	if err := ctx.ShouldBind(&req); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(req, err))
		return
	}

	res, err := c.customerService.GetFeedList(ctx, req)
	if err != nil {
		stark.Logger.Errorf(ctx, "customer/get-feed err:%s", err.Error())
		c.Fail(ctx, errcode.COMMON_DATA_NOT_EXIST, "接口返回异常")
		return
	}
	c.Success(ctx, res.List)
}

func (c *CustomerController) getInvestCompany(ctx *gin.Context) {
	c.setAnonymityUser(ctx)
	req := &dto.GetInvestCompanyRequest{}
	if err := ctx.ShouldBind(&req); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(req, err))
		return
	}

	res, err := c.customerService.GetInvestCompany(ctx, req)
	if err != nil {
		stark.Logger.Errorf(ctx, "customer/get-invest-company err:%s", err.Error())
		c.Fail(ctx, errcode.COMMON_DATA_NOT_EXIST, "接口返回异常")
		return
	}
	c.Success(ctx, res.List)
}

func (c *CustomerController) zxList(ctx *gin.Context) {
	c.setAnonymityUser(ctx)
	req := &dto.GetCustomerRiskCommonRequest{}
	if err := ctx.ShouldBind(req); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(req, err))
		return
	}

	resp, err := c.customerService.GetCustomerZxList(ctx, req)
	if err != nil {
		c.Fail(ctx, errcode.COMMON_ERROR, err.Error())
		return
	}

	c.Success(ctx, resp)
}

func (c *CustomerController) courtRegister(ctx *gin.Context) {
	c.setAnonymityUser(ctx)
	req := &dto.GetCustomerRiskCommonRequest{}
	if err := ctx.ShouldBind(req); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(req, err))
		return
	}

	resp, err := c.customerService.GetCustomerCourtRegister(ctx, req)
	if err != nil {
		c.Fail(ctx, errcode.COMMON_ERROR, err.Error())
		return
	}

	c.Success(ctx, resp)
}

func (c *CustomerController) lawSuit(ctx *gin.Context) {
	c.setAnonymityUser(ctx)
	req := &dto.GetCustomerRiskCommonRequest{}
	if err := ctx.ShouldBind(req); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(req, err))
		return
	}

	resp, err := c.customerService.GetCustomerLawSuit(ctx, req)
	if err != nil {
		c.Fail(ctx, errcode.COMMON_ERROR, err.Error())
		return
	}

	c.Success(ctx, resp)
}

func (c *CustomerController) taxIllegalList(ctx *gin.Context) {
	c.setAnonymityUser(ctx)
	req := &dto.GetCustomerRiskCommonRequest{}
	if err := ctx.ShouldBind(req); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(req, err))
		return
	}

	resp, err := c.customerService.GetCustomerTaxIllegal(ctx, req)
	if err != nil {
		c.Fail(ctx, errcode.COMMON_ERROR, err.Error())
		return
	}

	c.Success(ctx, resp)
}

func (c *CustomerController) fuzzySearch(ctx *gin.Context) {
	c.setAnonymityUser(ctx)
	req := &dto.FuzzySearchRequest{}
	if err := ctx.ShouldBind(req); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(req, err))
		return
	}

	result, err := c.customerService.FuzzySearch(ctx, req)
	if err != nil {
		c.Fail(ctx, errcode.COMMON_ERROR, err.Error())
		return
	}
	if len(result) == 0 {
		c.Success(ctx, []interface{}{})
		return
	}

	c.Success(ctx, result)
}

func (c *CustomerController) riskCount(ctx *gin.Context) {
	c.setAnonymityUser(ctx)
	req := &dto.GetCustomerRiskCountRequest{}
	if err := ctx.ShouldBind(req); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(req, err))
		return
	}

	resp, err := c.customerService.GetCustomerRiskCount(ctx, req)
	if err != nil {
		c.Fail(ctx, errcode.COMMON_ERROR, err.Error())
		return
	}

	c.Success(ctx, resp)
}
